#include "chatdialog.h"
#include "ui_chatdialog.h"
#include "tcphelper.h"
#include "QMessageBox"
#include "QTimer"
#include "sendfiledialog.h"
#include "receivefiledialog.h"
#include "tcptaskthread.h"

ChatDialog::ChatDialog(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::ChatDialog)
{
    ui->setupUi(this);
    ui->label_title->setText("您正在与"+myFriend.user_name+"进行对话");

    // 定时器，用于定期刷新聊天记录
    QTimer *timer = new QTimer(this);
    ChatDialog::connect(timer, &QTimer::timeout, this, &ChatDialog::refreshChatHistoryList);
    timer->start(200); // 每200毫秒刷新一次

    refreshChatHistoryList();
}

ChatDialog::~ChatDialog()
{
    delete ui;
}

void ChatDialog::refreshChatHistoryList()
{
    TcpHelper helper;

    // 创建并启动 TcpTaskThread
    TcpTaskThread *taskThread = new TcpTaskThread();
    taskThread->start();

    // 获取 TcpTask 对象
    TcpTask *task1 = taskThread->getTcpTask();

    ChatDialog::connect(task1, &TcpTask::ChatHistoryReady, [=](QList<ChatHistory> chatList){
        ui->textBrowser_chat->clear();

        for (const ChatHistory &chatItem : chatList) {
            QString formattedMessage = QString("[%1] %2: %3")
            .arg(chatItem.message_time.toString("hh:mm:ss"))
                .arg(chatItem.user_one_id == me.id ? "我" : myFriend.user_name)
                .arg(chatItem.message);

            ui->textBrowser_chat->append(formattedMessage);
        }
    });

    ChatDialog::connect(task1, &TcpTask::chatHistoryError, [=](){
        ui->textBrowser_chat->setText("无消息记录");
    });

    task1->sendCommand(helper.getUserIdForChatHistoryTask(me.id, myFriend.id));
}

void ChatDialog::on_toolButton_send_clicked()
{
    if(ui->lineEdit_send->text()!="")
    {
        chat.user_one_id = myId;
        chat.user_two_id = myFriend.id;
        chat.message = ui->lineEdit_send->text();
        chat.message_time = QTime::currentTime();
        chat.message_type = PURE_STRING;

        TcpHelper helper;

        // 创建并启动 TcpTaskThread
        TcpTaskThread *taskThread = new TcpTaskThread();
        taskThread->start();

        // 获取 TcpTask 对象
        TcpTask *task2 = taskThread->getTcpTask();

        ChatDialog::connect(task2, &TcpTask::InsertChatHistoryOk, [=](){
            refreshChatHistoryList();
            ui->lineEdit_send->clear();
        });

        ChatDialog::connect(task2, &TcpTask::InsertChatHistoryError, [=](){
            QMessageBox::warning(this, "Warning!", "消息发送失败", QMessageBox::Yes);
        });

        task2->sendCommand(helper.getInsertChatHistoryTask(chat));
    }
    else
    {//空消息
        QMessageBox::warning(this, "Warning!", "不能发送空消息", QMessageBox::Yes);
    }
}


void ChatDialog::on_pushButton_close_clicked()
{
    this->close();
}


void ChatDialog::on_toolButton_receiveFile_clicked()
{
    ReceiveFileDialog *rfd = new ReceiveFileDialog();
    rfd->show();
}


void ChatDialog::on_toolButton_sendFile_clicked()
{
    SendFileDialog *sfd = new SendFileDialog();
    sfd->show();
}

